/**
 * 
 */
package ch.unisi.inf.gui;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import java.io.*;
import java.awt.geom.*;
 
import java.awt.image.*;
import java.io.*;
 
import javax.media.jai.*;
import java.awt.image.renderable.ParameterBlock;
import com.sun.media.jai.codec.*;
import javax.imageio.*;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;

import java.awt.*;
 
public class Photo {
    /**
     *
     *
     */
    public static void resize(String original, String resized, int maxSize) {
        try{
            File originalFile = new File(original);
			ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
            Image i = ii.getImage();
            Image resizedImage = null;
            
            int iWidth = i.getWidth(null);
            int iHeight = i.getHeight(null);
            
        	if (iWidth > iHeight) {
                resizedImage = i.getScaledInstance(maxSize,(maxSize*iHeight)/iWidth,Image.SCALE_SMOOTH);
            } else {		
                resizedImage = i.getScaledInstance((maxSize*iWidth)/iHeight,maxSize,Image.SCALE_SMOOTH);
            }
 
            // This code ensures that all the 
            // pixels in the image are loaded.
            Image temp = new ImageIcon(resizedImage).getImage();
		
            // Create the buffered image.
            BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
		
            // Copy image to buffered image.
            Graphics g = bufferedImage.createGraphics();
		
            // Clear background and paint the image.
            g.setColor(Color.white);
            g.fillRect(0, 0, temp.getWidth(null),temp.getHeight(null));
            g.drawImage(temp, 0, 0, null);
            g.dispose();
 
            // sharpen
            float[] sharpenArray = { 0, -1, 0, -1, 5, -1, 0, -1, 0 };
 
            Kernel kernel = new Kernel(3, 3, sharpenArray);
            ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
            bufferedImage = cOp.filter(bufferedImage, null);
 
            /* write the jpeg to a file */
            File file = new File(resized);
            FileOutputStream out = new FileOutputStream(file);
 
            /* encodes image as a JPEG data stream */
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
 
            com.sun.image.codec.jpeg.JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
 
            // writeParam = new JPEGImageWriteParam(null);
            // writeParam.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
            //writeParam.setProgressiveMode(JPEGImageWriteParam.MODE_DEFAULT);
            param.setQuality(0.7f, true);
            encoder.setJPEGEncodeParam(param);
            encoder.encode(bufferedImage);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
 
  
    public static void main(String [] args) {
        long start = System.currentTimeMillis();
        
        System.out.println("done in " + (System.currentTimeMillis() - start) + " ms");
        JFrame frame = new JFrame("resize");
        JPanel panel = new JPanel();
        JPanel buttonPanel = new JPanel();
        
        frame.setLayout(new BorderLayout());
        JLabel one = new JLabel();
        JButton button = new JButton("resize");
        
        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
        
        
		one.setIcon(new ImageIcon("src/images/simone.jpg"));
		resize("src/images/simone.jpg", "src/images/resized.jpg", 200);
		
		one.setIcon(new ImageIcon("src/images/resized.jpg"));
		panel.add(one);
        
        frame.setSize(600, 600);
        frame.setVisible(true);
    }
}

